package com.service.admin;

import com.bean.bo.*;
import com.bean.pojo.Admin;
import com.bean.pojo.User;
import com.bean.vo.admin.AllAdminsVO;
import com.bean.vo.admin.Result;
import com.bean.vo.user.AllUserVO;
import com.dao.admin.AdminMapper;
import com.utils.MybatisUtils;
import com.utils.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 晓帆
 * @description:
 * @create: 2021-06-16 16:42
 **/
public class AdminServiceImplement implements AdminService {
    /**
     * @Author xiao-fan
     * @Description  登录
     * @Date 20:16 2021/6/16
     **/
    @Override
    public int login(AdminLogin adminLogin) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        long login = mapper.selectByAdminLogin(adminLogin);
        sqlSession.close();
        if (login!=0){
            return 200;
        }
        return 404;
    }
/**
 * @Author xiao-fan
 * @Description  展示所有管理员
 * @Date 20:16 2021/6/16
 **/
    @Override
    public List<AllAdminsVO> showAllAdmins() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        ArrayList<AllAdminsVO> allAdminsVOSList = new ArrayList<>();
        List<Admin> adminsList = mapper.selectAllAdmins();
        sqlSession.close();
        for (Admin admin : adminsList) {
            AllAdminsVO allAdminsVO = new AllAdminsVO(admin.getId(), admin.getUsername(), admin.getNickname(), admin.getPassword());
            allAdminsVOSList.add(allAdminsVO);
        }
        return allAdminsVOSList;
    }
/**
 * @Author xiao-fan
 * @Description  删除
 * @Date 20:16 2021/6/16
 **/
    @Override
    public int deleteAdmin(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int i = mapper.deleteAdmin(id);
        sqlSession.close();
        if (i==1){
            return 200;
        }
        return 404;
    }
/**
 * @Author xiao-fan
 * @Description  新增管理员
 * @Date 20:46 2021/6/16
 **/
    @Override
    public int addAdmin(AdminAdd adminAdd) {
        Admin admin = new Admin(null,adminAdd.getEmail(),adminAdd.getPwd(),adminAdd.getNickname());
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int i = 0;
        //用户名重复时候不允许插入
        try {
            i = mapper.addAdmin(admin);
        } catch (Exception e) {
            return 404;
        }
        sqlSession.close();
        if (i==1){
            return 200;
        }
        return 404;
    }

    @Override
    public UpdateAdmin selectUpdateAdmin(Integer id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        Admin admin = mapper.selectUpdateAdmin(id);
        sqlSession.close();
        UpdateAdmin updateAdmin = new UpdateAdmin(admin.getId(), admin.getUsername(), admin.getNickname(), admin.getPassword());
        return updateAdmin;
    }

    /**
     * 更新用户
     * @param updateAdmin
     * @return
     */
    @Override
    public int updateAdmin(UpdateAdmin updateAdmin) {
        Admin admin = new Admin(updateAdmin.getId(),updateAdmin.getEmail(),updateAdmin.getPwd(),updateAdmin.getNickname());
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int updateState = mapper.updateAdmin(admin);
        sqlSession.close();
        if (updateState==1){
            return 200;
        }
        return 404;
    }
/**
 * @Author xiao-fan
 * @Description  查询操作和联合查询
 * @Date 8:57 2021/6/17
 **/
    @Override
    public List<AllAdminsVO> selectByConditions(SelectAdmin selectAdmin) {
        Admin admin = new Admin(null,selectAdmin.getEmail(),null,selectAdmin.getNickname());
        //如果两个输入框都是空值的话显示所有管理员信息
        if (StringUtils.idEmpty(admin.getUsername())&&StringUtils.idEmpty(admin.getNickname())){
            List<AllAdminsVO> allAdminsVOS = showAllAdmins();
            return allAdminsVOS;
        }
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        ArrayList<AllAdminsVO> allAdminsVOArrayList = new ArrayList<>();
        List<Admin> adminList=null;
        //多条件联合查询
        if (!(StringUtils.idEmpty(admin.getNickname())||StringUtils.idEmpty(admin.getUsername()))){
            adminList=mapper.selectByJointConditions(admin);
        }
        //单个条件查询
        else {
             adminList = mapper.selectByConditions(admin);
        }

        sqlSession.close();
        for (Admin admin1 : adminList) {
            AllAdminsVO allAdminsVO = new AllAdminsVO(admin1.getId(), admin1.getUsername(), admin1.getNickname(), admin1.getPassword());
            allAdminsVOArrayList.add(allAdminsVO);
        }
        return allAdminsVOArrayList;
    }
/**
 * @Author xiao-fan
 * @Description  修改密码
 * @Date 23:00 2021/6/16
 **/
    @Override
    public int changePassword(ChangePasswordAdmin changePasswordAdmin) {
        Admin admin = new Admin(null, changePasswordAdmin.getAdminToken(), changePasswordAdmin.getNewPwd(),null);
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int changePasswordState = mapper.changePassword(admin);
        sqlSession.close();
        if (changePasswordState==1){
            return 200;
        }
        return 404;
    }
}
